package own.stu.jobgib.playown.alg.leetcode.editor.cn;

//有个内含单词的超大文本文件，给定任意两个单词，找出在这个文件中这两个单词的最短距离(相隔单词数)。如果寻找过程在这个文件中会重复多次，而每次寻找的单词不同，
//你能对此优化吗? 
//
// 示例： 
//
// 输入：words = ["I","am","a","student","from","a","university","in","a","city"], 
//word1 = "a", word2 = "student"
//输出：1 
//
// 提示： 
//
// 
// words.length <= 100000 
// 
// Related Topics 双指针 字符串 
// 👍 23 👎 0


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FindClosestLcci{
    public static void main(String[] args) {
        Solution solution = new FindClosestLcci().new Solution();
        solution.findClosest(new String[]{"I","am","a","from","student", "a","university","in","a","city"}, "a", "student");
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {

    public int findClosest(String[] words, String word1, String word2) {
        if(words == null || words.length < 2 || word1 == null || word2 == null){
            return -1;
        }

        int ans = Integer.MAX_VALUE;

        int idx1 = -1;
        int idx2 = -1;
        for(int i = 0; i < words.length; i ++){
            if(word1.equals(words[i])){
                idx1 = i;
            }
            if(word2.equals(words[i])){
                idx2 = i;
            }

            if(idx1 != -1 && idx2 != -1 && ans > Math.abs(idx1 - idx2)){
                ans = Math.abs(idx1 - idx2);
            }

        }
        return ans;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}
